﻿using System;
using System.Collections.Generic;

namespace XamlMvp
{
    public class HydratorBinder : IHydratorBinder
    {
        readonly IDictionary<RuntimeTypeHandle, object> binders;

        public HydratorBinder()
        {
            binders = new Dictionary<RuntimeTypeHandle, object>();
        }

        public void Add<T>(IHydratable<T> hydrator)
        {
            binders.Add(typeof(T).TypeHandle, hydrator);
        }

        public IHydratable<T> Get<T>()
        {
            return (IHydratable<T>)binders[typeof(T).TypeHandle];
        }

        public void DeHydrate<T>(IDictionary<string, object> state, T obj)
        {
            var hydrator = Get<T>();
            hydrator.FreezeObject(state, obj);
        }

        public void DeHydrate<TKind, T>(IDictionary<string, object> state, TKind kind)
        {
            var hydrator = Get<T>();
            hydrator.FreezeKind<TKind>(state, kind);
        }

        public T ReHydrate<T>(IDictionary<string, object> state)
        {
            var hydrator = Get<T>();
            return hydrator.HydrateObject(state);
        }
    }
}
